package cn.com.qingz.modules.user.service.impl;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

import cn.com.qingz.core.constantes.SystemConstantes;
import cn.com.qingz.core.exception.DicmfException;
import cn.com.qingz.modules.user.mapper.ResourceMapper;
import cn.com.qingz.modules.user.mapper.RoleMapper;
import cn.com.qingz.modules.user.mapper.UserMapper;
import cn.com.qingz.modules.user.model.Resource;
import cn.com.qingz.modules.user.model.Role;
import cn.com.qingz.modules.user.service.RoleService;
import cn.com.qingz.modules.user.vo.RoleAuthorityVo;
import cn.com.qingz.modules.user.vo.RoleCondition;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ResourceMapper resourceMapper;

    public List<Role> getAllRoles() {
        return roleMapper.getAll(new Role());
    }

    public List<Resource> getAllResource() {
        return roleMapper.getAllResource();
    }

    public List<Role> getUserRole(Long userid) {
        return roleMapper.queryRoleByUserId(userid);
    }

    public void doAddRole(Role role) {
        boolean isRepeat = checkRoleCode(role.getRole());
        if (isRepeat) {
            throw new DicmfException(SystemConstantes.ERROR, "角色代码重复");
        }
        String ids = role.getResource_ids();
        if (StringUtils.isEmpty(ids)) {
            throw new DicmfException(SystemConstantes.ERROR, "权限资源不能为空");
        }
        role.setStatus("Y");
        roleMapper.insert(role);
    }

    public void doUpdateRole(Role role) {
        String ids = role.getResource_ids();
        if (StringUtils.isEmpty(ids)) {
            throw new DicmfException(SystemConstantes.ERROR, "权限资源不能为空");
        }
        roleMapper.updateByPrimaryKey(role);
    }

    public void doDeleteRole(Role role) {
        roleMapper.delete(role);
    }

    public PageList<RoleAuthorityVo> queryRolePage(RoleCondition condition,
            int page, int limit) {
        return roleMapper.queryRolePage(condition, new PageBounds(page, limit));
    }

    public void doOnOff(Integer roleid, String status) {
        Role role = new Role();
        role.setId(roleid);

        Role myrole = roleMapper.getRoleById(role);
        myrole.setStatus(status);
        roleMapper.updateRole(myrole);

    }

    public boolean checkRoleCode(String code) {
        //		int n = roleMapper.countRoleCode(code);
        List<Role> r = userMapper.findRoleById("sdaf");
        System.out.println(r.get(0));
        //		try {
        //			List<Role>  x =    query.createSession().prepare("SELECT   ckd.* , item.real_quantity AS 'list[].real_quantity'  from wms_ckd ckd LEFT JOIN wms_ckd_item item on item.ckdid = ckd.id").queryForList(Role.class);
        //			for (Role role : x) {
        //				System.out.println(role.getDescription());
        //			}
        //			  System.out.println(x);
        ////			System.out.println(users);
        //		} catch (SQLException e) {
        //			// TODO Auto-generated catch block
        //			e.printStackTrace();
        //		}
        //		return n>0?true:false;
        return false;
    }

    public boolean checkResourceCode(String code) {
        int n = roleMapper.countRoleCode(code);
        return n > 0 ? true : false;
    }

    public void doDisableRoles(String roleids) {
        List<String> ids = Arrays.asList(roleids.split(","));
        roleMapper.updateRoleStatus(ids, "N");
    }

}
